package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.MmMaterial;
import com.farm.kaiping.po.MmChangeRecordPO;
import com.farm.kaiping.po.MmMaterialPO;
import com.farm.kaiping.mapper.MmMaterialMapper;
import com.farm.kaiping.service.MmChangeRecordService;
import com.farm.kaiping.service.MmMaterialService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 材料物料的说明与库存(MmMaterial)表服务实现类
 *
 * @author makejava
 * @since 2025-01-22 14:35:49
 */
@Service
@Slf4j
@AllArgsConstructor
public class MmMaterialServiceImpl extends ServiceImpl<MmMaterialMapper, MmMaterial> implements MmMaterialService {

	private final MmChangeRecordService mmChangeRecordService;

	private LambdaQueryWrapper<MmMaterial> generateQueryWrapper(MmMaterialPO mmMaterialPO) {
		LambdaQueryWrapper<MmMaterial> queryWrapper = new LambdaQueryWrapper<>();
		// _ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getId()), MmMaterial::getId, mmMaterialPO.getId());
		queryWrapper.in(ObjectUtil.isNotEmpty(mmMaterialPO.getIdList()), MmMaterial::getId, mmMaterialPO.getIdList());
		// 材料名称
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialName()), MmMaterial::getMaterialName, mmMaterialPO.getMaterialName());
		// 材料类型(数据字典)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialType()), MmMaterial::getMaterialType, mmMaterialPO.getMaterialType());
		// 材料类型(数据字典label)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialTypeLabel()), MmMaterial::getMaterialTypeLabel, mmMaterialPO.getMaterialTypeLabel());
		// 材料标签(数据字典)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialTags()), MmMaterial::getMaterialTags, mmMaterialPO.getMaterialTags());
		// 材料标签(数据字典label)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialTagLabels()), MmMaterial::getMaterialTagLabels, mmMaterialPO.getMaterialTagLabels());
		// 材料描述
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialDesc()), MmMaterial::getMaterialDesc, mmMaterialPO.getMaterialDesc());
		// 材料备注
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialNote()), MmMaterial::getMaterialNote, mmMaterialPO.getMaterialNote());
		// 库存数量
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialStock()), MmMaterial::getMaterialStock, mmMaterialPO.getMaterialStock());
		// 材料单位
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getMaterialUnit()), MmMaterial::getMaterialUnit, mmMaterialPO.getMaterialUnit());
		// 是否删除(0否1是)
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getDelFlag()), MmMaterial::getDelFlag, mmMaterialPO.getDelFlag());
		queryWrapper.eq(ObjectUtil.isEmpty(mmMaterialPO.getDelFlag()), MmMaterial::getDelFlag, 0);
		// 新增时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getCreateTime()), MmMaterial::getCreateTime, mmMaterialPO.getCreateTime());
		// 创建人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getCreateUserId()), MmMaterial::getCreateUserId, mmMaterialPO.getCreateUserId());
		// 创建人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getCreateUserName()), MmMaterial::getCreateUserName, mmMaterialPO.getCreateUserName());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getUpdateTime()), MmMaterial::getUpdateTime, mmMaterialPO.getUpdateTime());
		// 更新人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getUpdateUserId()), MmMaterial::getUpdateUserId, mmMaterialPO.getUpdateUserId());
		// 更新人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(mmMaterialPO.getUpdateUserName()), MmMaterial::getUpdateUserName, mmMaterialPO.getUpdateUserName());
		// 租户ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(mmMaterialPO.getTenantId()), MmMaterial::getTenantId, mmMaterialPO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(mmMaterialPO.getCreateTimeBetween(), 0)), MmMaterial::getCreateTime, StringRange.getOne(mmMaterialPO.getCreateTimeBetween(), 0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(mmMaterialPO.getCreateTimeBetween(), 1)), MmMaterial::getCreateTime, StringRange.getOne(mmMaterialPO.getCreateTimeBetween(), 1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(mmMaterialPO.getUpdateTimeBetween(), 0)), MmMaterial::getUpdateTime, StringRange.getOne(mmMaterialPO.getUpdateTimeBetween(), 0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(mmMaterialPO.getUpdateTimeBetween(), 1)), MmMaterial::getUpdateTime, StringRange.getOne(mmMaterialPO.getUpdateTimeBetween(), 1));

		queryWrapper.orderByDesc(MmMaterial::getCreateTime, MmMaterial::getId);
		return queryWrapper;
	}

	/**
	 * @describe 分页查询材料物料的说明与库存
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public Page<MmMaterial> getMmMaterialPage(Page page, MmMaterialPO mmMaterialPO) {
		LambdaQueryWrapper<MmMaterial> queryWrapper = this.generateQueryWrapper(mmMaterialPO);
		return this.baseMapper.selectPage(page, queryWrapper);
	}

	/**
	 * @describe 查询材料物料的说明与库存list
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public List<MmMaterial> getMmMaterialList(MmMaterialPO mmMaterialPO) {
		LambdaQueryWrapper<MmMaterial> queryWrapper = this.generateQueryWrapper(mmMaterialPO);
		return this.baseMapper.selectList(queryWrapper);
	}

	/**
	 * @describe 获取一条数据 材料物料的说明与库存
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public MmMaterial getMmMaterial(MmMaterialPO mmMaterialPO) {
		LambdaQueryWrapper<MmMaterial> queryWrapper = this.generateQueryWrapper(mmMaterialPO);
		queryWrapper.last("LIMIT 1");
		return this.baseMapper.selectOne(queryWrapper);
	}

	/**
	 * @describe 新增材料物料的说明与库存
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public Integer saveMmMaterial(MmMaterialPO mmMaterialPO) {
		if (ObjectUtil.isEmpty(mmMaterialPO.getMaterialName())) {
			throw new RuntimeException("材料名称为空");
		}
		if (ObjectUtil.isEmpty(mmMaterialPO.getMaterialType())) {
			throw new RuntimeException("材料类型(数据字典)为空");
		}
		if (ObjectUtil.isEmpty(mmMaterialPO.getMaterialTypeLabel())) {
			throw new RuntimeException("材料类型(数据字典label)为空");
		}
		if (ObjectUtil.isEmpty(mmMaterialPO.getMaterialStock())) {
			throw new RuntimeException("库存数量为空");
		}
		if (ObjectUtil.isEmpty(mmMaterialPO.getMaterialUnit())) {
			throw new RuntimeException("材料单位为空");
		}
		mmMaterialPO.setDelFlag(0);
		mmMaterialPO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		mmMaterialPO.setCreateTime(LocalDateTime.now());
		mmMaterialPO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		mmMaterialPO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		mmMaterialPO.setUpdateTime(LocalDateTime.now());
		MmMaterial mmMaterial = BeanUtil.copyProperties(mmMaterialPO, MmMaterial.class);
		this.save(mmMaterial);
		return mmMaterial.getId();
	}

	/**
	 * @describe 修改材料物料的说明与库存
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public String updateMmMaterial(MmMaterialPO mmMaterialPO) {
		if (ObjectUtil.isEmpty(mmMaterialPO.getId())) {
			throw new RuntimeException("id为空");
		}

		// 修改库存
		if (ObjectUtil.isNotEmpty(mmMaterialPO.getChangeType())) {

			MmChangeRecordPO mmChangeRecordPO = new MmChangeRecordPO();
			mmChangeRecordPO.setMaterialId(mmMaterialPO.getId());
			mmChangeRecordPO.setChangeType(mmMaterialPO.getChangeType());
			mmChangeRecordPO.setChangeTypeLabel(mmMaterialPO.getChangeTypeLabel());
			mmChangeRecordPO.setChangeNote(mmMaterialPO.getChangeNote());
			MmMaterial preData = this.getById(mmMaterialPO.getId());
			mmChangeRecordPO.setChangeNum(Math.abs(mmMaterialPO.getMaterialStock()-preData.getMaterialStock()));

			switch (mmMaterialPO.getChangeType()) {
				case 4:
				case 1:
					// 库存增加
					if (mmMaterialPO.getMaterialStock().compareTo(preData.getMaterialStock())<0){
						throw new RuntimeException("变更类型与数据不匹配");
					}
					break;
				case 2:
				case 3:
					// 库存减少
					if (mmMaterialPO.getMaterialStock().compareTo(preData.getMaterialStock())>0){
						throw new RuntimeException("变更类型与数据不匹配");
					}
					break;
			}
			mmChangeRecordService.saveMmChangeRecord(mmChangeRecordPO);
		}

		mmMaterialPO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		mmMaterialPO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		mmMaterialPO.setUpdateTime(LocalDateTime.now());
		MmMaterial mmMaterial = BeanUtil.copyProperties(mmMaterialPO, MmMaterial.class);
		this.updateById(mmMaterial);
		return "修改成功";
	}

	/**
	 * @describe 通过id删除材料物料的说明与库存
	 * @author makejava
	 * @date 2025-01-22 14:35:49
	 */
	@Override
	public String removeMmMaterial(MmMaterialPO mmMaterialPO) {
		if (ObjectUtil.isEmpty(mmMaterialPO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<MmMaterial> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(MmMaterial::getId, mmMaterialPO.getBatchDel());
		removeWrapper.set(MmMaterial::getDelFlag,1);
		removeWrapper.set(MmMaterial::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(MmMaterial::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(MmMaterial::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);
		return "删除成功";
	}
	
}

